﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class MissionTypeRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<MissionType> GetAllData()
        {
            return from MissionType in db.MissionTypes
                   orderby MissionType.MissionTypeCode descending
                   select MissionType;
        }


        public IQueryable<MissionType> GetDataByName(string searchField, string searchOper, string searchString)
        {
            var ps = db.MissionTypes.Where(p => p.MissionTypeCode.Contains(searchString));
            if (searchField == "MissionTypeDescription")
                ps = db.MissionTypes.Where(p => p.MissionTypeDescription.Contains(searchString));
            if (searchField == "MissionTypeCode")
                ps = db.MissionTypes.Where(p => p.MissionTypeCode.Contains(searchString));
            return ps.AsQueryable();

        }
        public IQueryable<MissionType> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllData().OrderByDescending(sortColumn);
                    else
                        return GetAllData().OrderBy(sortColumn);
                else
                    return GetAllData();
            }
        }



        public MissionType GetByID(string id)
        {
            return db.MissionTypes.SingleOrDefault(MissionType => MissionType.MissionTypeCode == id);
        }

        //Inset
        public void Add(MissionType MissionType)
        {
            //if (MissionType.StatusID == 0 )
            //    MissionType.StatusID = 1;
            MissionType.InsertedUser = HttpContext.Current.User.Identity.Name;
            MissionType.InsertedDate = DateTime.Now;
            MissionType.UpdatedUser = HttpContext.Current.User.Identity.Name;
            MissionType.UpdatedDate = DateTime.Now;
            MissionType.DataVersion = 1;

            db.MissionTypes.AddObject(MissionType);
        }

        //Update
        public void Update(MissionType MissionType)
        {

            MissionType originalMissionType = GetByID(MissionType.MissionTypeCode);

            MissionType.InsertedUser = originalMissionType.InsertedUser;
            MissionType.InsertedDate = originalMissionType.InsertedDate;
            MissionType.UpdatedUser = HttpContext.Current.User.Identity.Name;
            MissionType.UpdatedDate = DateTime.Now;
            MissionType.DataVersion = ++originalMissionType.DataVersion;
  
            db.MissionTypes.ApplyCurrentValues(MissionType);
        }

        //Delete
        public void Delete(MissionType MissionType)
        {
            db.MissionTypes.DeleteObject(MissionType);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}